/**
 * 
 */
package com.coo.tao.core.utils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName CommonUtil
 * @Description: TODO
 * @author lizhipeng
 * @date 2011-6-2 上午10:46:58
 *
 */
public class CommonUtil {

	 public static double add(double v1, double v2) {
		  BigDecimal b1 = new BigDecimal(Double.toString(v1));
		  BigDecimal b2 = new BigDecimal(Double.toString(v2));
		  return  b1.add(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	 }
	 public static double multiply(double v1, double v2) {
		 BigDecimal b1 = new BigDecimal(Double.toString(v1));
		 BigDecimal b2 = new BigDecimal(Double.toString(v2));
		 return  b1.multiply(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	 }
	 
	 public static String fitSql (String str, Map<String, Object> map){
		 Pattern pattern = Pattern.compile(":(\\w+)");
		 Matcher matcher = pattern.matcher(str);
		 StringBuffer sb = new StringBuffer();
		 while(matcher.find()){
			matcher.appendReplacement(sb, map.get(matcher.group(1)).toString());
		 }
		 matcher.appendTail(sb);
		 return sb.toString();
	 }
	 
	 public static void main(String[] args){
		 //System.out.println(CommonUtil.add(100.181212, 288.99232323));
		 Map<String, Object> map = new HashMap<String, Object>();
		 map.put("haha", "fff");
		 map.put("hhh", "fff");
		 map.put("oioi", "fff");
		 System.out.println(CommonUtil.fitSql("asdasdadasd:haha= asdasdadasd:hhh) asdadadasd:oioi" ,map));
	 }
	 
	 
}
